package uk.gov.swmprobation.cpvisibility;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	
	final static public String moveHintTitle = "Hint - Move Map";
	final static public String moveHintMessage = "Move the map until you can see where you took the picture. \n" +
			"Then tap the middle icon at the bottom.";
	
	final static public String markerHintTitle = "Hint - Place Marker";
	final static public String markerHintMessage = "Touch screen to place marker where you too the photo. \n" +
			"Tap the middle icon to move the map again";
	
	final static public String submitHintTitle = "Hint - Submit or Cancel";
	final static public String submitHintMessage = "Tap the first icon to submit your suggestion. \n" +
			"Tap the last icon to delete this suggestion.";
	
	final static public String mainHintTitle = "Hint - Start";
	final static public String mainHintMessage = "Welcome to the CP Visibility App.\n\n" +
			"Please use the Camera icon at the bottom of the page to take a photo and suggest it to CP " +
			"or the Magnifying Glass icon to search for nearby projects.";
	
	final static public String searchHintTitle = "Hint - Search";
	final static public String searchHintMessage = "Click on a mark to see details of a project. \n" +
			"The shadowed area has not been searched to seach it move the map to be centered where you want " +
			"to search and click on the magnifying glass";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
        addPreferencesFromResource(R.xml.preferences);
        Preference prefUserCode = (Preference)findPreference("pref_user_code");
        prefUserCode.setSummary(sharedPref.getString("pref_user_code", "General Public"));
        Preference prefWaitWifi = (Preference)findPreference("pref_wait_wifi");
        int wifiCode = Integer.parseInt(sharedPref.getString("pref_wait_wifi", "2"));
        prefWaitWifi.setSummary(wifiText(wifiCode));
        Preference prefResetHints = (Preference)findPreference("resetHints");
        prefResetHints.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				sharedPref.edit().putBoolean(moveHintTitle, true).commit();
				sharedPref.edit().putBoolean(markerHintTitle, true).commit();
				sharedPref.edit().putBoolean(submitHintTitle, true).commit();
				sharedPref.edit().putBoolean(mainHintTitle, true).commit();
				sharedPref.edit().putBoolean(searchHintTitle, true).commit();
				Toast.makeText(SettingsActivity.this, "All Hints will be shown", Toast.LENGTH_SHORT).show();
				return false;
			}
        	
        });
    }


	@Override
	protected void onResume() {
	    super.onResume();
	    // Set up a listener whenever a key changes
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
	    super.onPause();
	    // Unregister the listener whenever a key changes
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}
    
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPref,	String key) {
		if (key.equals("pref_user_code")) {
            Preference connectionPref = findPreference(key);
            connectionPref.setSummary(sharedPref.getString(key, "General Public"));
        }
		if (key.equals("pref_wait_wifi")) {
            Preference connectionPref = findPreference(key);
            int wifiCode = Integer.parseInt(sharedPref.getString("pref_wait_wifi", "2"));
            connectionPref.setSummary(wifiText(wifiCode));
        }
		
	}
	
	private String wifiText(int wifiCode){

		switch (wifiCode){
			case 1:
				return "Only send when Wifi available";
			case 2:
				return "Ask me when Wifi not available";
			case 3:
				return "Send even if Wifi not available";
			default:
				return "Ask me when Wifi not available";
		}	
	}
	
}
